<?php

namespace api\modules\ua\controllers;

use api\modules\ua\models\Baidu;
use api\modules\ua\models\Qunmai;
use api\modules\ua\models\Uuid;
use yii;
use api\modules\ua\models\Ping;
use api\modules\ua\models\Shops;
use yii\web\Response;
use yii\filters\auth\QueryParamAuth;
use yii\rest\ActiveController;

class EventController extends ActiveController
{
    public $modelClass = 'api\modules\ua\models\User';
    public $app_prefix = 'ua';
    public $version,$conf;

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $class     = 'api\modules\ua\models\User';
        $behaviors['authenticator'] = ['class' => QueryParamAuth::className(Yii::$app->user->identityClass=$class)];
        $behaviors['contentNegotiator']['formats'] = ['application/json' => Response::FORMAT_JSON];
        return $behaviors;
    }

    public function init()
    {
        $this->version = Yii::$app->params[$this->app_prefix]['version'];
    }

    public function actions()
    {
        $actions = parent::actions();
        // 注销系统自带的实现方法
        unset($actions['index'],$actions['view'], $actions['create'], $actions['update'], $actions['delete']);
        return $actions;
    }


    // ========= 获取初始化数据 =========
    public function actionInitData(){
        // 验证数据提交方法
        if(!Yii::$app->request->isPost){$data['code'] = 400;return $data;
        }else{$params = Yii::$app->request->bodyParams;}

        // 验证参数
        if(!isset($params['uuid'])){$data['code'] = 20000;return $data;}

        // 验证uuid，如果不存在则返回404 ---------------------------
        $shop = new Shops();
        $shopRes = $shop->getOne($params['uuid']);
        if(!$shopRes){
            // 记录到临时表里
            $model = new Uuid();
            $model->create($params['uuid']);

            // 后台操作
            $data['code'] = 404;return $data;
        }

        // 获取版本 ----------------------------------------------
//        $data['version'] = $this->version;

        // 获取百度token ------------------------------------------
        $baidu = new Baidu();
        $data['baiduToken'] = $baidu->getBaiduToken();

        // 获取群脉数据 ------------------------------------------
        // 版本
        $qm = new Qunmai();
        $res = $qm->buildData($shopRes->code);
        $data['storeCode']      = $shopRes->code;
        $data['version']        = $res['version'];
        $data['wechatQrCode']   = $res['wxcode']->wechatQrCode;
        $data['promotion']      = $res['promotion'];
        $data['goods']          = $res['goods'];

        $data['version']['rights'] = Yii::$app->params[$this->app_prefix]['rights_ver'];
        if(isset($params['rights_ver']) && $params['rights_ver']!==$data['version']['rights']){
            $data['rights'] = require_once (__DIR__.'/../config/user-rights.php');
        }

        return $data;
    }

    // ========= 检查设备UUID是否在库 =========
    public function actionCheckUuid(){
        //验证数据提交方法
        if(!Yii::$app->request->isPost){$data['code'] = 400;return $data;
        }else{$params = Yii::$app->request->bodyParams;}

        //验证参数
        if(!isset($params['uuid'])){$data['code'] = 20000;return $data;}

        $shop = new Shops();
        $res = $shop->getOne($params['uuid']);
        if($res){
            $data['code'] = 10000;
            return $data;
        }else{
            $data['code'] = 404;
            return $data;
        }
    }


    // ========= 记录ping =========
    public function actionSavePing(){
        //验证数据提交方法
        if(!Yii::$app->request->isPost){$data['code'] = 400;return $data;
        }else{$params = Yii::$app->request->bodyParams;}

        //验证参数
        if(!isset($params['uuid'])){$data['code'] = 20000;return $data;}

        $ping = new Ping();
        $ping->create($params['uuid']);
        $data['code'] = 10000;
        return $data;
    }


    // ========= 检查设备是否在线 =========
    // 标记设备状态:在线/离线
    // 离线状态时发送邮件
    public function actionCheckOnline(){
        if(!Yii::$app->request->isPost){$data['code'] = 400;return $data;
        }else{$params = Yii::$app->request->bodyParams;}

//        if(!isset($params['uuids'])){$data['code'] = 20000;return $data;}

        $model  = new Shops();
        $res = $model->checkOnline();

        if($res['status']===false){
            $data['code']   = 405;
            $data['res']    = $res;
        }else{
            $data['code']   = 10000;
            $data['res']    = $res;
        }
        return $data;
    }
}
